package frontend.components.other;

import backend.model.admin.Employee;
import backend.model.admin.Team;
import backend.model.other.User;
import shared.other.UserRolesDefinition;
import backend.service.admin.EmployeeService;
import backend.service.admin.TeamService;
import backend.service.other.UserService;
import frontend.core.TimeTrackingWebApplication;
import frontend.core.TimeTrackingWebSession;
import frontend.model.base.UIModel;
import frontend.components.base.ComponentFactory;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.panel.Panel;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class EmployeeSelectionPanel<UIM extends UIModel> extends Panel {

    private User loggedInUser;

    public EmployeeSelectionPanel(String id, UIM model, UserInputCallableAction<Employee> onSelectionAction, String labelResourceKey, boolean isNullValid) {
        super(id);

        String loggedInUsername = (String) TimeTrackingWebSession.get().getAttribute(TimeTrackingWebSession.LOGIN_USERNAME_ATTR);
        loggedInUser = TimeTrackingWebApplication.getService(UserService.class).findUserByUsername(loggedInUsername);

        boolean isForeman = UserRolesDefinition.UserRole.FOREMAN.equals(loggedInUser.getRole());
        boolean isAdmin = UserRolesDefinition.UserRole.ADMIN.equals(loggedInUser.getRole());

        List<Employee> availableEmployees = new ArrayList<Employee>();
        if (isForeman) {
            availableEmployees.addAll(getAllActiveEmployeesForForeman());
        } else if (isAdmin) {
            availableEmployees.addAll(getAllActiveEmployees());
        } else {
            availableEmployees.add(loggedInUser.getEmployee());
        }
        WebMarkupContainer employeeComponent = ComponentFactory.createDropDownChoiceComponent(
                "employee", labelResourceKey, model, availableEmployees,
                new DBModelChoiceRenderer<Employee>(), isNullValid, onSelectionAction);
        employeeComponent.setVisible(isAdmin || isForeman);

        add(employeeComponent);
    }

    private List<Employee> getAllActiveEmployees() {
        List<Employee> availableEmployees = new ArrayList<Employee>();
        availableEmployees.addAll(TimeTrackingWebApplication.getService(EmployeeService.class).findAllActiveEmployees());
        Collections.sort(availableEmployees);
        return availableEmployees;
    }

    private List<Employee> getAllActiveEmployeesForForeman() {
        List<Employee> availableEmployees = new ArrayList<Employee>();
        List<Team> managedTeams = TimeTrackingWebApplication.getService(TeamService.class).findAllActiveTeamsOfForeman(loggedInUser.getEmployee().getForeman().getId());
        for (Team team : managedTeams) {
            for (Employee employee : team.getEmployees()) {
                if (employee.getIsActive()) {
                    availableEmployees.add(employee);
                }
            }
        }
        Collections.sort(availableEmployees);
        return availableEmployees;
    }
}
